﻿using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DatabaseTransfer.Forms
{
    public partial class LogConfig : Form
    {
        public LogConfig()
        {
            InitializeComponent();
        }

        public void SetupLogs()
        {
            ILog[] loggers = LogManager.GetCurrentLoggers();

            int count = 1;
            TabPage page = null;
            Point position = new Point(5, 5);
            foreach (ILog log in loggers)
            {
                if (count == 1)
                {
                    page = AddTab();
                    position = new Point(5, 5);
                }
                position = AddControl(log, page, position);
                count++;
                if (count > 6)
                {
                    count = 0;
                }
            }
        }

        private Point AddControl(ILog log, TabPage page, Point pos)
        {
            Label label = new Label();
            label.Text = log.Logger.Name;
            label.SetBounds(pos.X, pos.Y, 275, 24);
            label.TextAlign = ContentAlignment.MiddleRight;
            
            page.Controls.Add(label);

            ComboBox box = new ComboBox();
            box.Items.AddRange(new object[] { "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" });
            box.SetBounds(label.Bounds.X + label.Bounds.Width + 10, label.Bounds.Y, 100, 24);
            box.FormattingEnabled = true;
            
            page.Controls.Add(box);

            Button button = new Button();
            button.Text = "Set";
            button.SetBounds(box.Bounds.X + box.Bounds.Width + 10, box.Bounds.Y, 75, 24);
            button.Enabled = false;
            button.Click += (sender, e) =>
            {
                button.Enabled = false;
                ILog logger = LogManager.GetLogger(label.Text);

                log4net.Core.Level logLevel = null;

                switch (box.SelectedIndex)
                {
                    case 0:
                        logLevel = log4net.Core.Level.All;
                        break;
                    case 1:
                        logLevel = log4net.Core.Level.Debug;
                        break;
                    case 2:
                        logLevel = log4net.Core.Level.Info;
                        break;
                    case 3:
                        logLevel = log4net.Core.Level.Warn;
                        break;
                    case 4:
                        logLevel = log4net.Core.Level.Error;
                        break;
                    case 5:
                        logLevel = log4net.Core.Level.Fatal;
                        break; 
                    case 6:
                        logLevel = log4net.Core.Level.Off;
                        break;
                }

                ((log4net.Repository.Hierarchy.Logger)logger.Logger).Level = logLevel;
            };

            page.Controls.Add(button);

            log4net.Core.Level level = ((log4net.Repository.Hierarchy.Logger)log.Logger).Level;

            int value = level == null ? int.MinValue : level.Value;

            switch (value)
            {
                case int.MinValue:
                    box.SelectedIndex = 0;
                    break;
                case 30000:
                    box.SelectedIndex = 1;
                    break;
                case 40000:
                    box.SelectedIndex = 2;
                    break;
                case 60000:
                    box.SelectedIndex = 3;
                    break;
                case 70000:
                    box.SelectedIndex = 4;
                    break;
                case 110000:
                    box.SelectedIndex = 5;
                    break;
                case int.MaxValue:
                default:
                    box.SelectedIndex = 6;
                    break;
            }

            box.SelectedIndexChanged += (sender, e) => { button.Enabled = true; };

            Point p = new Point();
            p.X = 5;
            p.Y = label.Bounds.Y + 30;

            return p;
        }

        private TabPage AddTab()
        {
            string title = "Log Page" + (LoggerControl.TabCount + 1);
            TabPage tabPage = new TabPage(title);
            LoggerControl.TabPages.Add(tabPage);
            return tabPage;
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
